/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import java.util.Iterator;
import java.util.List;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import static org.zkoss.zk.ui.util.Clients.log;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import times.TimesDAO;
import times.persistence.entities.AnimalSpecies;
import times.persistence.entities.PriceSource;

/**
 *
 * @author nektarios
 */
public class PriceSourceUI extends SelectorComposer<Component> {

    @Wire
    Textbox nameTxt;
    @Wire
    Textbox contactNameTxt;
    @Wire
    Textbox phoneTxt;
    @Wire
    Textbox emailTxt;

    @Wire
    Listbox animalSpeciesLst ;
    
    
    @Wire
    Listbox priceSourceLst;

    ListModelList<PriceSource> priceSourceLml;
    ListModelList<AnimalSpecies> animalSpeciesLml;
    
    private boolean newPriceSource;
    private PriceSource priceSource;
    private TimesDAO tdao;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        tdao = new TimesDAO();
        priceSourceLml = new ListModelList(tdao.findPriceSourceEntities());
        priceSourceLst.setModel(priceSourceLml);
        
        animalSpeciesLml = new ListModelList(tdao.findAnimalSpeciesEntities());
        animalSpeciesLst.setModel(animalSpeciesLml);
        

    }

    @Listen("onClick = #newBtn")
    public void onNewBtnClick() {
        newPriceSource = true;
        priceSource = new PriceSource();
        Messagebox.show("Συμπληρωστε και πατηστε αποθηκευση");
        clearUI();
    }

    @Listen("onClick = #saveBtn")
    public void onSaveBtnClick() throws Exception {
        getUI();
        if (newPriceSource) {
            log("new recorde");
            
            tdao.createPriceSource(priceSource);
        } else {
            tdao.editPriceSource(priceSource);
        }

        Messagebox.show("H εγγραφή αποθηκευτηκε");
        refreshUI();
    }

    
    @Listen("onSelect = #priceSourceLst")
    public void onPriceSourceSelect() {
        clearUI();
        priceSource = priceSourceLml.getSelection().iterator().next();
        setUI();
    }
    
    public void getUI() {
        priceSource.setName(nameTxt.getValue());
        priceSource.setContactName(contactNameTxt.getValue());
        priceSource.setPhone(phoneTxt.getValue());
        priceSource.setEmail(emailTxt.getValue());
        priceSource.setAnimalSpeciesId(animalSpeciesLml.getSelection().iterator().next());
        
        
    }

    private void setUI() {
        nameTxt.setValue(priceSource.getName());
        contactNameTxt.setValue(priceSource.getContactName());
        phoneTxt.setValue(priceSource.getPhone());
        emailTxt.setValue(priceSource.getEmail());
        animalSpeciesLml.addToSelection(priceSource.getAnimalSpeciesId());
    }
    
    private void clearUI() {
        nameTxt.setValue("");
        contactNameTxt.setValue("");
        phoneTxt.setValue("");
        emailTxt.setValue("");
    }

    private void refreshUI() {
        Executions.sendRedirect("");
    }

}
